package com.forlove.app.manager.async;

import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;

import com.forlove.app.ForloveApplication;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.utils.NetworkUtils;
import com.forlove.app.common.utils.StringUtils;
import com.forlove.app.manager.ContactsManager;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.login.entity.ContactsInfoEntity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yjl on 2017/10/21.
 */

public class UpLoadContactsAsync extends AsyncTaskLoader<List<ContactsInfoEntity>> {
    public static final int ASYNC_ID = 20002;

    public UpLoadContactsAsync(Context context) {
        super(context);
    }

    @Override
    public List<ContactsInfoEntity> loadInBackground() {
        List<ContactsInfoEntity> list = ContactsManager.getContectInfo(getContext());
        if (list.size() > 0 && NetworkUtils.instance(getContext()).isNetworkAvailable()) {
            StringBuilder friendsNumber = new StringBuilder();
            StringBuilder nameBuilder = new StringBuilder();
            for (int i = 0; i < list.size(); i++) {
                ContactsInfoEntity contactsInfoEntity = list.get(i);
                friendsNumber.append(contactsInfoEntity.getNumber());
                if (StringUtils.isNotEmpty(contactsInfoEntity.getName())
                        && !StringUtils.containsEmoji(contactsInfoEntity.getName())) {
                    nameBuilder.append(contactsInfoEntity.getName());
                } else {
                    nameBuilder.append(" ");
                }
                if (i < list.size() - 1) {
                    friendsNumber.append(",");
                    nameBuilder.append(",");
                }
            }
            Map param = new HashMap();
            param.put("list", friendsNumber.toString());
            param.put("namelist", nameBuilder.toString());
//            param.put("isupdate","1");//1是分批上传
            try {
                NetworkManager.getInstance().loadDataByPost(ServerManager.ACTION_UPLOAD_CONTACTS, param, new ICallBack() {
                    @Override
                    public void onSuccess(int statusCode, String result) {
                        LogUtil.e("上传通讯录:" + result);
                    }

                    @Override
                    public void onFail(int statusCode, String result) {
                        LogUtil.e("上传通讯录:" + result);

                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (list.size() > 0) {
            ContactsManager.saveContactsFriend(ForloveApplication.getInstance(), list);
        }
        return list;
    }

    @Override
    protected void onStartLoading() {
        super.onStartLoading();
        forceLoad();
    }

}
